package com.lijiaxing.service.impl;

import com.lijiaxing.mapper.userMapper;
import com.lijiaxing.pojo.PermissionRes;
import com.lijiaxing.pojo.Result;
import com.lijiaxing.pojo.UserRes;
import com.lijiaxing.pojo.verificationCode;
import com.lijiaxing.service.userService;
import com.lijiaxing.utils.JwtMaker;
import com.lijiaxing.utils.PojoToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class userServiceImpl implements userService {
    @Autowired
    private userMapper um;

    @Override
    public Result setRegister(String utel, String upwd, String ucode) {
        //验证码有效
        if (queryVerification(utel, ucode)) {
            //未注册
            if (queryOneUser(utel)) {
                //注册成功
                if (um.setOneRegister(utel, upwd) > 0) {
                    return Result.success(null, "注册成功，请登录");
                }
                //注册失败
                return Result.error("", "注册失败");
            }
            //已注册
            return Result.success("", "用户已注册，请登录");

        }
        //验证码无效
        return Result.error("注册失败", "验证码无效");
    }

    @Override
    public boolean queryVerification(String utel, String ucode) {
        verificationCode[] datas = um.queryVerification(utel, ucode);
        return datas.length > 0;
    }

    /**
     * {@code
     *
     * @return {ture:未查询到,false:查询到}
     * }
     */
    @Override
    public boolean queryOneUser(String utel) {
        UserRes.User[] users = um.queryOneUserByUtel(utel);
        return users.length == 0;
    }

    @Override
    public Result<Object> login(String utel, String upwd) {
        boolean b = queryOneUser(utel);
        if (!b) {
            UserRes.User[] users = um.queryOneUserByArgs(utel, upwd);
            if (users.length > 0) {
                Map<String, Object> map = PojoToMap.objectToMap(users[0]);
                String token = JwtMaker.enCoder(map, 3);
                return Result.success(token, "登录成功");
            }
            return Result.error(null, "密码错误");
        }
        return Result.error(null, "用户未注册");
    }

    @Override
    public Result<Object> addPermission(PermissionRes.Permission p) {
        PermissionRes.Permission[] permissions = um.queryOnePermission(p.getPname());
        if (permissions.length == 0) {
            int i = um.addPermission(p.getPname(), p.getPermissions(), p.getPermissionsName());
            if (i == 1) {
                //新增成功返回新数据
                return Result.success(null, "权限新增成功");
            }
        }
        return Result.error(null, "权限名称重复");
    }

    @Override
    public Result queryTenPermission(int pageNum, int pageSize) {
        pageNum = (pageNum - 1) * 10;
        PermissionRes.Permission[] permissions = um.queryOnePagePermission(pageNum, pageSize);
        int total = um.queryAllPermissionCount();
        return Result.success(new PermissionRes(total, permissions), "OK");
    }

    @Override
    public Result updatePermission(PermissionRes.Permission p) {
        PermissionRes.Permission[] permissions = um.queryOnePermissionByNameAndId(p.getPid(), p.getPname());
        if (permissions.length == 0) {
            //无重复
            int i = um.updatePermission(p.getPid(), p.getPname(), p.getPermissions(), p.getPermissionsName());
            if (i == 1) {
                //更新成功返回新数据
                return Result.success(null, "权限更新成功");
            }
        }
        return Result.error(null, "权限名称重复");
    }

    @Override
    public Result queryAllPermission() {
        PermissionRes.Permission[] permissions = um.queryAllPermissions();
        return Result.success(permissions, "OK");
    }
}
